package com.xydtech.workflow.service.impl;

import com.xydtech.common.core.text.Convert;
import com.xydtech.workflow.domain.*;
import com.xydtech.workflow.mapper.*;
import com.xydtech.workflow.service.IWflFlowversionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 流程版本信息Service业务层处理
 *
 * @author JHON
 * @date 2021-04-11
 */
@Service
public class WflFlowversionServiceImpl implements IWflFlowversionService {
    @Autowired
    private WflFlowversionMapper wflFlowversionMapper;
    @Autowired
    private WflApprauthorMapper wflApprauthorMapper;
    @Autowired
    private WflFlowstepMapper wflFlowstepMapper;
    @Autowired
    private WflStepDocMapper wflStepDocMapper;
    @Autowired
    private WflSteplogicMapper wflSteplogicMapper;
    @Autowired
    private WflSteppageMapper wflSteppageMapper;
    @Autowired
    private WflStepskipMapper wflStepskipMapper;

    /**
     * 查询流程版本信息
     *
     * @param versionId 流程版本信息ID
     * @return 流程版本信息
     */
    @Override
    public WflFlowversion selectWflFlowversionById(Long versionId) {
        return wflFlowversionMapper.selectWflFlowversionById(versionId);
    }

    /**
     * 查询流程版本信息列表
     *
     * @param wflFlowversion 流程版本信息
     * @return 流程版本信息
     */
    @Override
    public List<WflFlowversion> selectWflFlowversionList(WflFlowversion wflFlowversion) {
        return wflFlowversionMapper.selectWflFlowversionList(wflFlowversion);
    }

    /**
     * 新增流程版本信息
     *
     * @param wflFlowversion 流程版本信息
     * @return 结果
     */
    @Override
    public int insertWflFlowversion(WflFlowversion wflFlowversion) {
        return wflFlowversionMapper.insertWflFlowversion(wflFlowversion);
    }

    /**
     * 复制流程版本信息
     *
     * @param wflFlowversion 流程版本信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int copyWflFlowVersion(WflFlowversion wflFlowversion) {
        int i = 0;
        long copyVersionId = wflFlowversion.getVersionId();
        //复制流程版本信息
        i = wflFlowversionMapper.copyWflFlowVersion(wflFlowversion);
        //复制流程节点
        WflFlowstep wflFlowstep = new WflFlowstep();
        wflFlowstep.setVersionId(wflFlowversion.getVersionId());
        wflFlowstep.setCopyVersionId(copyVersionId);
        wflFlowstepMapper.copyWflFlowstep(wflFlowstep);
        //复制流程审批权限
        WflApprauthor wflApprauthor = new WflApprauthor();
        wflApprauthor.setVersionId(wflFlowversion.getVersionId());
        wflApprauthor.setCopyVersionId(copyVersionId);
        wflApprauthorMapper.copyWflApprauthor(wflApprauthor);
        //复制流程页面配置
        WflSteppage wflSteppage = new WflSteppage();
        wflSteppage.setVersionId(wflFlowversion.getVersionId());
        wflSteppage.setCopyVersionId(copyVersionId);
        wflSteppageMapper.copyWflSteppage(wflSteppage);
        //复制流程节点跳过条件配置
        WflStepskip wflStepskip = new WflStepskip();
        wflStepskip.setVersionId(wflFlowversion.getVersionId());
        wflStepskip.setCopyVersionId(copyVersionId);
        wflStepskipMapper.copyWflStepskip(wflStepskip);
        //复制流程节点逻辑配置
        WflSteplogic wflSteplogic = new WflSteplogic();
        wflSteplogic.setVersionId(wflFlowversion.getVersionId());
        wflSteplogic.setCopyVersionId(copyVersionId);
        wflSteplogicMapper.copyWflSteplogic(wflSteplogic);
        //复制流程节点影像资料配置
        WflStepDoc wflStepDoc = new WflStepDoc();
        wflStepDoc.setVersionId(wflFlowversion.getVersionId());
        wflStepDoc.setCopyVersionId(copyVersionId);
        wflStepDocMapper.copyWflStepDoc(wflStepDoc);

        return i;
    }

    /**
     * 修改流程版本信息
     *
     * @param wflFlowversion 流程版本信息
     * @return 结果
     */
    @Override
    public int updateWflFlowversion(WflFlowversion wflFlowversion) {
        return wflFlowversionMapper.updateWflFlowversion(wflFlowversion);
    }

    /**
     * 根据流程号修改流程版本状态
     *
     * @param wflFlowversion 流程版本信息
     * @return 结果
     */
    @Override
    public int updateWflFlowVersionByFlowId(WflFlowversion wflFlowversion) {
        return wflFlowversionMapper.updateWflFlowVersionByFlowId(wflFlowversion);
    }

    /**
     * 删除流程版本信息对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWflFlowversionByIds(String ids) {
        return wflFlowversionMapper.deleteWflFlowversionByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除流程版本信息信息
     *
     * @param versionId 流程版本信息ID
     * @return 结果
     */
    @Override
    public int deleteWflFlowversionById(Long versionId) {
        return wflFlowversionMapper.deleteWflFlowversionById(versionId);
    }
}
